package com.example.l3.ktm;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.*;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class browserdownload  extends Activity {
	
	private Button btnBack;
	private Button btnUp;
	private GridView gridView;
	private GridViewAdapter gridViewAdapter;
	private String filePath;
	private String root;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browser);
		
		btnBack = (Button)findViewById(R.id.btnBack);
		btnUp = (Button)findViewById(R.id.btnUp);
		gridView = (GridView)findViewById(R.id.gridView1);

		filePath = Environment.getExternalStorageDirectory().getPath();
		
	    gridViewAdapter = new GridViewAdapter(this, GetFilterReadableFile(filePath));
		gridView.setAdapter(gridViewAdapter);

		gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
				List<File> listFile = gridViewAdapter.listFiles;
				File file = listFile.get(arg2);

				if(file.isDirectory() && file.canRead() && !file.isHidden() && file.exists()) {
					gridViewAdapter = new GridViewAdapter(browserdownload.this, GetFilterReadableFile(file.getPath()));
					gridView.setAdapter(gridViewAdapter);
					root = file.getParentFile().getPath();
				}
			}
		});
		
		btnBack.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				setResult(1);
				finish();
			}
		});
		
		btnUp.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				String upPath = GetParentPath();
//				
//				if(upPath != null && upPath != "" && upPath != "/") {
//					File file = new File(upPath);
//					if(file.isDirectory() && file.canRead() && !file.isHidden() && file.exists()) {
//						gridViewAdapter = new GridViewAdapter(browserdownload.this, GetFilterReadableFile(file.getPath()));
//						gridView.setAdapter(gridViewAdapter);
//					}
//				}
				
				File file = new File(root);
				

					if(file.isDirectory() && file.canRead() && !file.isHidden() && file.exists()) {
						gridViewAdapter = new GridViewAdapter(browserdownload.this, GetFilterReadableFile(file.getPath()));
						gridView.setAdapter(gridViewAdapter);
						root = GetParentPath();
					}

			}
		});
	}
	
	protected String GetParentPath() {
		List<File> listFile = gridViewAdapter.listFiles;
		if(listFile.size() > 0) {
			try {
				File file = listFile.get(0);
				String path = file.getParentFile().getParentFile().getPath();
				return path;
			} catch (NullPointerException e) {
				// TODO: handle exception
				return "/";
			}
		} else {
			return "/";
		}
	}
	protected List<File> GetFilterReadableFile(String path) {
		List<File> listFile = new ArrayList<File>();
		File file = new File(path);
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			File temp = files[i];
			if(temp.canRead() && temp.exists() && (temp.isDirectory() || temp.isFile()) && !temp.isHidden()) {
				listFile.add(temp);
			}
		}
		return listFile;
	}

	public static class ViewHolder {
		public ImageView thumbnailImage;
		public TextView thumbName;
	}
	
	public class GridViewAdapter extends BaseAdapter {

		public List<File> listFiles;
		private Context context;
		private ViewHolder view;
		
		public GridViewAdapter(Context context, List<File> files) {
			listFiles = files;
			this.context = context;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return listFiles.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return listFiles.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			if(convertView == null) {
				view = new ViewHolder();
				convertView = inflater.inflate(R.layout.gridview_cell, null);
				
				view.thumbnailImage = (ImageView) convertView.findViewById(R.id.imgThumb);
				view.thumbName = (TextView) convertView.findViewById(R.id.txtThumbName);
				
				convertView.setTag(view);
			} else {
				view = (ViewHolder) convertView.getTag();
			}
			
			if(listFiles != null) {
				File file = (File) listFiles.get(position);
				view.thumbName.setText(file.getName());
				if(file != null && file.canRead() && file.exists() && !file.isHidden()) {
					
					if(file.isDirectory()) {
						if(file.getName().equals("Compressed")) {
							view.thumbnailImage.setImageResource(R.drawable.folder_library);
						} else if(file.getName().equals("Document")) {
							view.thumbnailImage.setImageResource(R.drawable.folder_documents);
						} else if(file.getName().equals("Music")) {
							view.thumbnailImage.setImageResource(R.drawable.folder_music);				
						} else if(file.getName().equals("Program")) {
							view.thumbnailImage.setImageResource(R.drawable.folder_applications);
						} else if(file.getName().equals("Video")) {
							view.thumbnailImage.setImageResource(R.drawable.folder_movies);
						} else if(file.getName().equals("Picture")) {
							view.thumbnailImage.setImageResource(R.drawable.folder_photos);
						} else {
							view.thumbnailImage.setImageResource(R.drawable.folder);
						}
					} else if(file.isFile()) {
						CheckFileType(file.getName());
					}
				}
			}
			return convertView;
		}

		public String getExtensionFile(String filename){
			String[] arrayComponents = filename.split("\\.");
			String extensionName = arrayComponents[arrayComponents.length - 1];
			return extensionName;
		}
		
		public void CheckFileType(String fileName) {
			String extensionName = getExtensionFile(fileName);
			if (extensionName.equals("zip")) {//file
				view.thumbnailImage.setImageResource(R.drawable.file_zip);
			} else if(extensionName.equals("rar")) {
				view.thumbnailImage.setImageResource(R.drawable.file_rar);
			} else if(extensionName.equals("doc")) {
				view.thumbnailImage.setImageResource(R.drawable.file_doc);
			} else if(extensionName.equals("docx")) {
				view.thumbnailImage.setImageResource(R.drawable.file_docx);
			} else if(extensionName.equals("ppt")) {
				view.thumbnailImage.setImageResource(R.drawable.file_ppt);
			} else if(extensionName.equals("pptx")) {
				view.thumbnailImage.setImageResource(R.drawable.file_pptx);
			} else if(extensionName.equals("xls")) {
				view.thumbnailImage.setImageResource(R.drawable.file_xls);
			} else if(extensionName.equals("xlsx")) {
				view.thumbnailImage.setImageResource(R.drawable.file_xlsx);
			} else if(extensionName.equals("pdf")) {
				view.thumbnailImage.setImageResource(R.drawable.file_pdf);
			} else if(extensionName.equals("txt")) {
				view.thumbnailImage.setImageResource(R.drawable.file_text);
			} else if(extensionName.equals("mp3")) {
				view.thumbnailImage.setImageResource(R.drawable.file_mp3);
			} else if(extensionName.equals("wav")) {
				view.thumbnailImage.setImageResource(R.drawable.file_wav);
			} else if(extensionName.equals("wma")) {
				view.thumbnailImage.setImageResource(R.drawable.file_wma);
			} else if(extensionName.equals("midi") || extensionName.equals("mid")) {
				view.thumbnailImage.setImageResource(R.drawable.file_midi);
			} else if(extensionName.equals("avi")) {
				view.thumbnailImage.setImageResource(R.drawable.file_avi);
			} else if(extensionName.equals("mpeg")) {
				view.thumbnailImage.setImageResource(R.drawable.file_mpeg);
			} else if(extensionName.equals("mov")) {
				view.thumbnailImage.setImageResource(R.drawable.file_mov);
			} else if(extensionName.equals("wmv")) {
				view.thumbnailImage.setImageResource(R.drawable.file_wmv);
			} else if(extensionName.equals("jpeg") || extensionName.equals("jpg")) {
				view.thumbnailImage.setImageResource(R.drawable.file_jpeg);
			} else if(extensionName.equals("png")) {
				view.thumbnailImage.setImageResource(R.drawable.file_png);
			} else if(extensionName.equals("bmp")) {
				view.thumbnailImage.setImageResource(R.drawable.file_bmp);
			} else if(extensionName.equals("exe") || extensionName.equals("bat")) {
				view.thumbnailImage.setImageResource(R.drawable.file_ini);
			} else if(extensionName.equals("apk")){
				view.thumbnailImage.setImageResource(R.drawable.ic_launcher);
			} else {
				view.thumbnailImage.setImageResource(R.drawable.file_generic);
			}
		}
	}
}
